﻿using System;
using Microsoft.Practices.ObjectBuilder;
using DataContracts;
using System.Collections.Generic;
using System.Web.UI.WebControls;


namespace TrainingWorkshop.MVPWebsite.Authority.Views
{
    public partial class UpdateAuthority : Microsoft.Practices.CompositeWeb.Web.UI.Page, IUpdateAuthorityView
    {
        private UpdateAuthorityPresenter _presenter;

        private static String authorityId;

        #region Property

        public int AuthorityId { get; set; }
        public string AuthorityName
        {
            get
            {
                return UpdateName.Text;
            }
            set
            {
                UpdateName.Text = value;
            }
        }
        public int LevelId
        {
            get
            {
                int levelId;
                int.TryParse(UpdateLevel.SelectedValue, out levelId);
                return levelId;
            }
        }
        public string LevelName
        {
            get
            {
                return UpdateLevel.SelectedItem.Text;
            }
            set
            {
                UpdateLevel.SelectedItem.Text = value;
            }
        }

        public IList<AuthorityLevelDTO> AuthorityLevelList
        {
            set
            {
                this.UpdateLevel.DataSource = value;
                this.UpdateLevel.DataTextField = "LevelName";
                this.UpdateLevel.DataValueField = "LevelId";
                this.UpdateLevel.DataBind();
                UpdateLevel.Items.Insert(0, new ListItem("Please Selected", "0"));
            }
        }

        public AuthorityLevelDTO SelectedAuthority
        {
            set
            {
                for (int i = 0; i < this.UpdateLevel.Items.Count; i++)
                {
                    if (this.UpdateLevel.Items[i].Value == value.LevelId.ToString())
                    {
                        this.UpdateLevel.Items[i].Selected = true;
                    }
                }
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //Put AuthorityName into TextBox
            authorityId = Request.QueryString["id"].ToString();
            AuthorityId = int.Parse(authorityId);
            if (!this.IsPostBack)
            {
                _presenter.OnViewInitialized();
            }
            _presenter.OnViewLoaded();
        }


        [CreateNew]
        public UpdateAuthorityPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            this._presenter.UpdateAuthority();
        }

        public void Redirect(string url)
        {
            Response.Redirect(url);
        }

        public void ShowOperationMessage(string message)
        {
            System.Windows.Forms.MessageBox.Show(message, "Error");
        }
	}
}

